(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.apSiteProfile.updateVerifyController', updateVerifyController);
	updateVerifyController.$inject = [ '$scope', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService', 'apSiteProfileService' ];
	function updateVerifyController($scope, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, apSiteProfileService) {
		var ctrl = this, apSiteProfileInfo = $scope.$stateParams.params, errorMessage = {};
		//ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.apSiteProfile = {};
		
		ctrl.init = function() {
			var params = {
					taskId : apSiteProfileInfo.taskId
			};
			// init cust prop define
			apSiteProfileService.initCustProp(layoutConfig, $scope, ctrl, true);
			//
			// init general
			apSiteProfileService.initGeneral($scope, ctrl).then(function() {
				authService.updateVerify(params).then(function(response) {
					var initApSiteProfile = response.data.model.newModel;
					ctrl.action = "updateVerify";
					apSiteProfileService.initLayoutConfigAndModel($scope, ctrl, initApSiteProfile, ctrl.action).then(function() {
						// ctrl.apSiteProfile = initApSiteProfile; done in initLayoutConfig
						ctrl.apSiteProfile.taskId = response.data.task.taskId || apSiteProfileInfo.taskId;
						//
						ctrl.apSiteProfileOld = response.data.model.oldModel || {};
					});
				});
			});
		};

		ctrl.updateVerifyApprove = function() {
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.approve', args : ['AP Site Profile']} ],
				'btnCallback' : {
					'ok' : function() {
						authService.updateVerifyApprove(ctrl.apSiteProfile).then(approveSuccessful, approveFailed);
					}
				}
			});
		};

		ctrl.updateVerifyReject = function() {
			modalService.openPromptDialogs({
				'btnCallback' : {
					'ok' : function(comments) {
						ctrl.apSiteProfile.comments = comments;
						authService.updateVerifyReject(ctrl.apSiteProfile).then(rejectSuccessful, rejectFailed);
					}
				}
			});
		};

		function rejectSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.reject' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.apsiteprofile.list',
					'state' : 'cda.apSiteProfile.list',
					'params' : {}
				}]
			});
		}

		function rejectFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function approveSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.approve' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.apsiteprofile.list',
					'state' : 'cda.apSiteProfile.list',
					'params' : {}
				}]
			});
		}

		function approveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
		
	}
})(angular);